package cn.tangsihai.shoptrolley.service.impl;

import cn.tangsihai.basic.exception.MyException;
import cn.tangsihai.basic.service.impl.BasicServiceImpl;
import cn.tangsihai.basic.util.LoginContextUtil;
import cn.tangsihai.pet.domain.Pet;
import cn.tangsihai.pet.mapper.PetMapper;
import cn.tangsihai.pet.service.IPetService;
import cn.tangsihai.shoptrolley.domain.ShopCart;
import cn.tangsihai.shoptrolley.mapper.ShopCartMapper;
import cn.tangsihai.shoptrolley.service.IShopCartService;
import cn.tangsihai.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class ShopCartServiceImpl extends BasicServiceImpl<ShopCart> implements IShopCartService{
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private ShopCartMapper shopCartMapper;
    /**
     * 添加到购物车
     * @param petId
     */
    @Override
    @Transactional
    public void addCart(Long petId) {
        // 校验参数
        if (petId == null){
            throw new MyException("请选择商品");
        }
        // 根据宠物id查询上架的宠物数据
        Pet pet = petMapper.findByIdAndState(petId, Pet.ONSALE_IN_PET);
        if (pet == null){
            throw new MyException("你选择的商品未上架，请重新选择");
        }

        //

        // 准备购物车数据
        ShopCart shopCart = new ShopCart();
        shopCart.setPetId(petId);
        shopCart.setName(pet.getName());
        shopCart.setResources(pet.getResources());
        shopCart.setSaleprice(pet.getSaleprice());
        shopCart.setShopId(pet.getShop_id());
        shopCart.setType(pet.getType().getName());
        // 获取请求头中的用户登录数据
        Logininfo logininfo = LoginContextUtil.getLoginContext();
        shopCart.setLogininfoId(logininfo.getId());
        shopCart.setState(ShopCart.WAIT_PAY); // 初始状态，1待购买
        // 将数据同步到数据库
        shopCartMapper.addCart(shopCart);
    }

    /**
     * 加载登录用户的购物车数据
     * @return
     */
    @Override
    public List<ShopCart> findAllByLogininfoId() {
        // 获取登录用户的登录信息
        Logininfo logininfo = LoginContextUtil.getLoginContext();
        if (logininfo.getId() == null){
            throw new MyException("请先登录");
        }
        return shopCartMapper.findAllByLogininfoIdAndState(logininfo.getId(), ShopCart.WAIT_PAY);
    }

    /**
     * 移除购物车中数据
     * @param id
     */
    @Override
    public void remove(Long id) {
        // 校验参数
        if(id == null){
            throw new MyException("请选择要移除的商品");
        }
        // 根据id查询出购物车中展示状态的数据并修改状态为删除状态
        ShopCart shopCart = shopCartMapper.findByIdAndState(id, ShopCart.WAIT_PAY);
        if (shopCart == null){
            throw new MyException("该商品不存在,请刷新购物车");
        }
        // 修改状态并更新数据库
        shopCart.setState(ShopCart.DELETEED);
        shopCartMapper.update(shopCart);
    }

    /**
     * 计算选择数据的总价
     * @param ids
     * @return
     */
    @Override
    public BigDecimal getPrices(List<Long> ids) {
        // 校验参数
        if (ids.isEmpty()){
            throw new MyException("请选择商品");
        }
        BigDecimal prices = shopCartMapper.batchFindById(ids, ShopCart.WAIT_PAY);
        return prices;
    }
}
